package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ProvinceBean;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.ChooseBankAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.ChooseDialogAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/6/11.
 * Desc   :
 */

public class ChooseDialog extends DialogFragment {

    @BindView(R.id.recyclerview_choose_dialog)
    RecyclerView recyclerviewChooseDialog;
    @BindView(R.id.txt_title)
    TextView txtTitle;

    private PIDListener pidListener;
    private ChooseDialogAdapter chooseDialogAdapter;

    private List<ProvinceBean> provinceBeanList;
    private List<String> bankList = new ArrayList<>();
    private String type;

    public static ChooseDialog newInstance(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        ChooseDialog chooseDialog = new ChooseDialog();
        chooseDialog.setArguments(bundle);
        return chooseDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            this.type = arguments.getString("type");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        //去掉dialog的标题，需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        assert window != null;
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8f);
        lp.height = (int) (dm.heightPixels * 0.7f);
        //设置dialog的动画
//        lp.windowAnimations = R.style.BottomDialogAnimStyle;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_choose, null);
        ButterKnife.bind(this, view);
        txtTitle.setText(type);
        recyclerviewChooseDialog.setLayoutManager(new LinearLayoutManager(getContext()));
        setData();
        return view;
    }

    private void setData() {
        if (type.equals("请选择银行")) {
            ChooseBankAdapter chooseBankAdapter = new ChooseBankAdapter(getContext(), R.layout.item_dialog_choose, bankList);
            recyclerviewChooseDialog.setAdapter(chooseBankAdapter);
            chooseBankAdapter.setOnItemClickListener((adapter, position) -> {
                dismiss();
                pidListener.getBank(bankList.get(position));
            });
        } else {
            chooseDialogAdapter = new ChooseDialogAdapter(getContext(), R.layout.item_dialog_choose, provinceBeanList);
            recyclerviewChooseDialog.setAdapter(chooseDialogAdapter);
            chooseDialogAdapter.setOnItemClickListener((adapter, position) -> {
                dismiss();
                pidListener.getPid(provinceBeanList.get(position));
            });
        }
    }

    public void setData(List<ProvinceBean> provinceBeanList) {
        this.provinceBeanList = provinceBeanList;
    }

    public void setBankData(List<String> list) {
        this.bankList = list;
    }

    public interface PIDListener {
        void getPid(ProvinceBean provinceBean);

        void getBank(String bankName);
    }

    public void setPIDListener(PIDListener pidListener) {
        this.pidListener = pidListener;
    }

}
